{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b8c6483",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有股票目录和 ipynb 文件已生成完毕！\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import shutil\n",
    "import json\n",
    "\n",
    "# -------------------------\n",
    "# 配置参数\n",
    "# -------------------------\n",
    "TEMPLATE_DIR = \"../00000temp\"  # 模板目录\n",
    "TARGET_PARENT_DIR = \"../demo\"  # 股票目录生成位置，与模板目录同级\n",
    "STOCK_LIST = [\n",
    "    {\"code\": \"601328\", \"name\": \"交通银行\"},\n",
    "    {\"code\": \"601628\", \"name\": \"中国人寿\"},\n",
    "    {\"code\": \"601919\", \"name\": \"中远海控\"},\n",
    "    {\"code\": \"600415\", \"name\": \"小商品城\"},\n",
    "    {\"code\": \"002489\", \"name\": \"浙江永强\"},\n",
    "    {\"code\": \"601808\", \"name\": \"中海油服\"},\n",
    "    {\"code\": \"000876\", \"name\": \"新希望\"},\n",
    "    {\"code\": \"603678\", \"name\": \"火炬电子\"},\n",
    "    {\"code\": \"603678\", \"name\": \"火炬电子\"},\n",
    "    {\"code\": \"002801\", \"name\": \"微光股份\"},\n",
    "]\n",
    "\n",
    "\n",
    "# -------------------------\n",
    "# 遍历股票列表\n",
    "# -------------------------\n",
    "for stock in STOCK_LIST:\n",
    "    stock_code = stock[\"code\"]\n",
    "    stock_name = stock[\"name\"]\n",
    "    \n",
    "    # 新目录路径\n",
    "    new_dir = os.path.join(TARGET_PARENT_DIR, stock_name)\n",
    "    \n",
    "    # 复制模板目录\n",
    "    if os.path.exists(new_dir):\n",
    "        shutil.rmtree(new_dir)\n",
    "    shutil.copytree(TEMPLATE_DIR, new_dir)\n",
    "    \n",
    "    # 遍历新目录下的所有 .ipynb 文件\n",
    "    for root, dirs, files in os.walk(new_dir):\n",
    "        for file in files:\n",
    "            if file.endswith(\".ipynb\"):\n",
    "                file_path = os.path.join(root, file)\n",
    "                \n",
    "                # 读取 ipynb 文件（JSON 格式）\n",
    "                with open(file_path, \"r\", encoding=\"utf-8\") as f:\n",
    "                    notebook = json.load(f)\n",
    "                \n",
    "                # 修改 code 单元\n",
    "                for cell in notebook.get(\"cells\", []):\n",
    "                    if cell.get(\"cell_type\") == \"code\":\n",
    "                        new_source = []\n",
    "                        for line in cell.get(\"source\", []):\n",
    "                            line = line.replace('stock_code = \"000001\"', f'stock_code = \"{stock_code}\"')\n",
    "                            line = line.replace('stock_name = \\'中国平安\\'', f'stock_name = \\'{stock_name}\\'')\n",
    "                            new_source.append(line)\n",
    "                        cell[\"source\"] = new_source\n",
    "                \n",
    "                # 保存修改后的文件\n",
    "                with open(file_path, \"w\", encoding=\"utf-8\") as f:\n",
    "                    json.dump(notebook, f, ensure_ascii=False, indent=1)\n",
    "\n",
    "print(\"所有股票目录和 ipynb 文件已生成完毕！\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
